{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "title-section"
      },
      "source": [
        "# SUTRA x Portkey: The Gateway Cookbook\n",
        "\n",
        "<img src=\"https://play-lh.googleusercontent.com/_O9p4Z4yucA2NLmZBu9mTJCuBwXeT9NcbtrDN6I8gKlkIPRySV0adOmbyipjSj9Gew\" width=\"150\">\n",
        "\n",
        "<img src=\"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRX8V9wZZ69LrmJjm8VmbSw_2FnBbUOtXDAAQ&s\" width=\"150\">\n",
        "\n",
        "\n",
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/11FLZl0gScugIpXpC0M9tMDWn9C_osZ9A?usp=sharing)\n",
        "\n",
        "This notebook provides a simple, easy-to-follow guide for using Sutra-v2 models with Portkey's AI Gateway. We'll focus on the basics to get you up and running quickly."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kfUMivYLrMSe"
      },
      "source": [
        "## Get Your API Keys\n",
        "\n",
        "Before you begin, make sure you have:\n",
        "\n",
        "1. A SUTRA API key (Get yours at [TWO AI's SUTRA API](https://app.portkey.ai/api-keys))\n",
        "2. A Portkey API Key (Get yours at [PORTKEY API](https://www.two.ai/sutra/api))\n",
        "2. Basic familiarity with Python and Jupyter notebooks\n",
        "\n",
        "This notebook is designed to run in Google Colab, so no local Python installation is required."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "setup-section"
      },
      "source": [
        "## 1. Install Required Packages\n",
        "\n",
        "First, let's install the Portkey and OpenAI packages:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "install-packages",
        "outputId": "0ecad99f-ee1a-4282-c2cc-b665655836d8"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\u001b[?25l   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/756.9 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K   \u001b[91m━━━━━━━━━━━━\u001b[0m\u001b[90m╺\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m235.5/756.9 kB\u001b[0m \u001b[31m7.1 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K   \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m \u001b[32m747.5/756.9 kB\u001b[0m \u001b[31m13.4 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m756.9/756.9 kB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip install -q portkey-ai openai"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "api-keys"
      },
      "source": [
        "## 2. Set Up Your API Keys\n",
        "\n",
        "You'll need both a Sutra API key and a Portkey API key. For security, we'll use environment variables:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "setup-keys"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from google.colab import userdata\n",
        "\n",
        "# Set your API keys\n",
        "sutra_api_key = userdata.get('SUTRA_API_KEY')\n",
        "portkey_api_key = userdata.get('PORTKEY_API_KEY')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "initialize-portkey"
      },
      "source": [
        "## 3. Initialize Portkey with Sutra-v2\n",
        "\n",
        "Now we'll set up Portkey to work with Sutra-v2 models:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "portkey-init"
      },
      "outputs": [],
      "source": [
        "from portkey_ai import Portkey\n",
        "\n",
        "\n",
        "# Initialize Portkey client with Sutra as the provider\n",
        "client = Portkey(\n",
        "    api_key=portkey_api_key,\n",
        "    provider=\"openai\",  # Using openai provider for Sutra\n",
        "    base_url=\"https://api.two.ai/v2\",  # Sutra API endpoint\n",
        "    Authorization=sutra_api_key  # Sutra API key\n",
        ")\n",
        "\n",
        "print(\"Portkey client initialized with Sutra-v2!\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2x3bJTsL_fSV"
      },
      "outputs": [],
      "source": [
        "# Make a request through your AI Gateway\n",
        "response = client.chat.completions.create(\n",
        "    messages=[{\"role\": \"user\", \"content\": \"Who is founder of sutra?\"}],\n",
        "    model=\"sutra-v2\"\n",
        ")\n",
        "response.choices[0].message.content"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "helper-function"
      },
      "source": [
        "## 4. Create a Simple Helper Function\n",
        "\n",
        "Let's create a simple function to generate text using Sutra-v2 models through Portkey:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "helper-code"
      },
      "outputs": [],
      "source": [
        "def ask_sutra(prompt, model=\"sutra-v2\", temperature=0.7, max_tokens=500):\n",
        "    \"\"\"Simple function to get responses from Sutra-v2 via Portkey\"\"\"\n",
        "    response = client.chat.completions.create(\n",
        "        model=model,\n",
        "        messages=[\n",
        "            {\"role\": \"user\", \"content\": prompt}\n",
        "        ],\n",
        "        temperature=temperature,\n",
        "        max_tokens=max_tokens\n",
        "    )\n",
        "    return response.choices[0].message.content"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "basic-example"
      },
      "source": [
        "## 5. Try a Simple Example\n",
        "\n",
        "Let's test our setup with a basic prompt:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "test-basic"
      },
      "outputs": [],
      "source": [
        "# Test with a simple prompt\n",
        "simple_prompt = \"Explain the importance of AI in modern healthcare in India.\"\n",
        "response = ask_sutra(simple_prompt)\n",
        "print(response)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "multilingual"
      },
      "source": [
        "## 6. Try Multilingual Capabilities\n",
        "\n",
        "Sutra-v2 excels at Indian languages. Let's test it with a Hindi prompt:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "hindi-example"
      },
      "outputs": [],
      "source": [
        "# Hindi prompt\n",
        "hindi_prompt = \"भारत में कृत्रिम बुद्धिमत्ता (AI) के महत्व के बारे में बताएं।\"\n",
        "hindi_response = ask_sutra(hindi_prompt)\n",
        "print(hindi_response)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "creative-writing"
      },
      "source": [
        "## 7. Creative Writing Example\n",
        "\n",
        "Let's try a creative writing prompt:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "creative-example"
      },
      "outputs": [],
      "source": [
        "# Creative writing prompt\n",
        "creative_prompt = \"Write a short poem about the beauty of the Himalayas.\"\n",
        "creative_response = ask_sutra(creative_prompt)\n",
        "print(creative_response)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "basic-portkey-features"
      },
      "source": [
        "## 8. Using Basic Portkey Features\n",
        "\n",
        "Let's try a simple Portkey feature - automatic retries for reliability:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "retry-example"
      },
      "outputs": [],
      "source": [
        "# Configure retries\n",
        "retry_config = {\n",
        "    \"retry\": {\n",
        "        \"attempts\": 3,  # Retry up to 3 times\n",
        "    }\n",
        "}\n",
        "\n",
        "# Create a client with retry configuration\n",
        "retry_client = client.with_options(config=retry_config)\n",
        "\n",
        "# Function to use the retry-enabled client\n",
        "def ask_with_retry(prompt, model=\"sutra-v2\"):\n",
        "    response = retry_client.chat.completions.create(\n",
        "        model=model,\n",
        "        messages=[{\"role\": \"user\", \"content\": prompt}],\n",
        "        max_tokens=500\n",
        "    )\n",
        "    return response.choices[0].message.content"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "C60fUQE3peLj"
      },
      "source": [
        "### Test with a prompt\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-uge8fehpdPr"
      },
      "outputs": [],
      "source": [
        "# Test with a prompt\n",
        "retry_prompt = \"What are the major festivals celebrated in different regions of India?\"\n",
        "retry_response = ask_with_retry(retry_prompt)\n",
        "print(retry_response)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "simple-caching"
      },
      "source": [
        "## 9. Simple Caching for Better Performance\n",
        "\n",
        "Let's implement basic caching to improve response times for repeated queries:\n",
        "\n",
        "**Note :- Caching is only available on Portkey's hosted gateway and enterprise plans.**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "cache-example"
      },
      "outputs": [],
      "source": [
        "# Configure simple caching\n",
        "cache_config = {\n",
        "    \"cache\": {\n",
        "        \"mode\": \"simple\" \n",
        "    }\n",
        "}\n",
        "\n",
        "# Create a client with caching\n",
        "cached_client = client.with_options(config=cache_config)\n",
        "\n",
        "# Function to demonstrate caching\n",
        "def test_simple_caching(prompt):\n",
        "    import time\n",
        "\n",
        "    print(\"First request (cache miss):\")\n",
        "    start_time = time.time()\n",
        "    response1 = cached_client.chat.completions.create(\n",
        "        model=\"sutra-v2\",\n",
        "        messages=[{\"role\": \"user\", \"content\": prompt}],\n",
        "        max_tokens=500\n",
        "    )\n",
        "    time1 = time.time() - start_time\n",
        "    print(f\"Time taken: {time1:.2f} seconds\")\n",
        "    print(f\"Response: {response1.choices[0].message.content[:150]}...\\n\")\n",
        "\n",
        "    print(\"Second request with same prompt (cache hit):\")\n",
        "    start_time = time.time()\n",
        "    response2 = cached_client.chat.completions.create(\n",
        "        model=\"sutra-v2\",\n",
        "        messages=[{\"role\": \"user\", \"content\": prompt}],\n",
        "        max_tokens=500\n",
        "    )\n",
        "    time2 = time.time() - start_time\n",
        "    print(f\"Time taken: {time2:.2f} seconds\")\n",
        "    print(f\"Response: {response2.choices[0].message.content[:150]}...\")\n",
        "\n",
        "    if time2 < time1:\n",
        "        print(f\"\\nCaching improved response time by {time1/time2:.1f}x!\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LWBLtKMApYEL"
      },
      "source": [
        "### Test caching\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "4-moy_aWpWzt"
      },
      "outputs": [],
      "source": [
        "# Test caching\n",
        "cache_prompt = \"Explain the concept of artificial intelligence to a 10-year-old child.\"\n",
        "test_simple_caching(cache_prompt)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "conclusion"
      },
      "source": [
        "## 10. Conclusion\n",
        "\n",
        "In this simple guide, you've learned how to:\n",
        "\n",
        "1. Set up Portkey with Sutra-v2 models\n",
        "2. Create a simple helper function for generating text\n",
        "3. Test Sutra-v2's capabilities with different types of prompts\n",
        "4. Use basic Portkey features like retries and caching\n",
        "\n",
        "This integration gives you the best of both worlds: Sutra-v2's powerful language capabilities (especially for Indian languages) and Portkey's reliability features.\n",
        "\n"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
